home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / Mesa-2.2 / widgets-mesa / demos / mcube.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-31  |  10.0 KB  |  394 lines

  1. /* mcube.c -- Demo program for the Mesa widget
  2.    Copyright (C) 1995 Thorsten.Ohl @ Physik.TH-Darmstadt.de
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2 of the License, or
  7.    (at your option) any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; see the file COPYING.  If not, write to
  16.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.    $Id: mcube.c,v 1.14 1996/09/27 18:08:10 ohl Exp $
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <stdarg.h>
  24. #include <math.h>
  25. #include <X11/X.h>
  26. #include <X11/Intrinsic.h>
  27. #include <X11/StringDefs.h>
  28. #include <X11/Shell.h>
  29. #ifndef __GLX_MOTIF
  30. #include <X11/Xaw/Command.h>
  31. #include <X11/Xaw/Form.h>
  32. #include <GL/xmesa.h>
  33. #include <GL/gl.h>
  34. #include <GL/MesaDrawingArea.h>
  35. #else /* __GLX_MOTIF */
  36. #include <Xm/PushB.h>
  37. #include <Xm/Form.h>
  38. #include <GL/xmesa.h>
  39. #include <GL/gl.h>
  40. #include <GL/MesaMDrawingArea.h>
  41. #define GLwMakeCurrent GLwMMakeCurrent
  42. #endif /* __GLX_MOTIF */
  43.  
  44. static char *RCS_Id =
  45. "@(#) $Id: mcube.c,v 1.14 1996/09/27 18:08:10 ohl Exp $";
  46.  
  47. static GLint Black, Red, Green, Blue;
  48.  
  49. void quit_function (Widget, XtPointer, XtPointer);
  50.  
  51. void
  52. quit_function (Widget w, XtPointer closure, XtPointer call_data)
  53. {
  54.   exit (0);
  55. }
  56.  
  57. static void
  58. draw_cube (void)
  59. {
  60.   /* X faces */
  61.   glIndexi (Red);
  62.   glColor3f (1.0, 0.0, 0.0);
  63.   glBegin (GL_POLYGON);
  64.   {
  65.     glVertex3f (1.0, 1.0, 1.0);
  66.     glVertex3f (1.0, -1.0, 1.0);
  67.     glVertex3f (1.0, -1.0, -1.0);
  68.     glVertex3f (1.0, 1.0, -1.0);
  69.   }
  70.   glEnd ();
  71.  
  72.   glBegin (GL_POLYGON);
  73.   {
  74.     glVertex3f (-1.0, 1.0, 1.0);
  75.     glVertex3f (-1.0, 1.0, -1.0);
  76.     glVertex3f (-1.0, -1.0, -1.0);
  77.     glVertex3f (-1.0, -1.0, 1.0);
  78.   }
  79.   glEnd ();
  80.  
  81.   /* Y faces */
  82.   glIndexi (Green);
  83.   glColor3f (0.0, 1.0, 0.0);
  84.   glBegin (GL_POLYGON);
  85.   {
  86.     glVertex3f (1.0, 1.0, 1.0);
  87.     glVertex3f (1.0, 1.0, -1.0);
  88.     glVertex3f (-1.0, 1.0, -1.0);
  89.     glVertex3f (-1.0, 1.0, 1.0);
  90.   }
  91.   glEnd ();
  92.  
  93.   glBegin (GL_POLYGON);
  94.   {
  95.     glVertex3f (1.0, -1.0, 1.0);
  96.     glVertex3f (-1.0, -1.0, 1.0);
  97.     glVertex3f (-1.0, -1.0, -1.0);
  98.     glVertex3f (1.0, -1.0, -1.0);
  99.   }
  100.   glEnd ();
  101.  
  102.   /* Z faces */
  103.   glIndexi (Blue);
  104.   glColor3f (0.0, 0.0, 1.0);
  105.   glBegin (GL_POLYGON);
  106.   {
  107.     glVertex3f (1.0, 1.0, 1.0);
  108.     glVertex3f (-1.0, 1.0, 1.0);
  109.     glVertex3f (-1.0, -1.0, 1.0);
  110.     glVertex3f (1.0, -1.0, 1.0);
  111.   }
  112.   glEnd ();
  113.  
  114.   glBegin (GL_POLYGON);
  115.   {
  116.     glVertex3f (1.0, 1.0, -1.0);
  117.     glVertex3f (1.0, -1.0, -1.0);
  118.     glVertex3f (-1.0, -1.0, -1.0);
  119.     glVertex3f (-1.0, 1.0, -1.0);
  120.   }
  121.   glEnd ();
  122. }
  123.  
  124. GLfloat xrot, yrot, zrot;
  125.  
  126. static void
  127. init_display (Widget w)
  128. {
  129.   xrot = yrot = zrot = 0.0;
  130.  
  131.   GLwMakeCurrent (w);
  132.  
  133.   glClearColor (0.0, 0.0, 0.0, 0.0);
  134.   glClearIndex (Black);
  135.   glClearDepth (10.0);
  136.  
  137.   glMatrixMode (GL_PROJECTION);
  138.   glLoadIdentity ();
  139.   glFrustum (-1.0, 1.0, -1.0, 1.0, 1.0, 10.0);
  140.   glTranslatef (0.0, 0.0, -3.0);
  141.  
  142.   glMatrixMode (GL_MODELVIEW);
  143.   glLoadIdentity ();
  144.  
  145.   glCullFace (GL_BACK);
  146.   glEnable (GL_CULL_FACE);
  147.  
  148.   glShadeModel (GL_FLAT);
  149. }
  150.  
  151. static void
  152. display (Widget w)
  153. {
  154.   GLwMakeCurrent (w);
  155.   glClear (GL_COLOR_BUFFER_BIT);
  156.   glPushMatrix ();
  157.   {
  158.     glRotatef (xrot, 1.0, 0.0, 0.0);
  159.     glRotatef (yrot, 0.0, 1.0, 0.0);
  160.     glRotatef (zrot, 0.0, 0.0, 1.0);
  161.     draw_cube ();
  162.   }
  163.   glPopMatrix ();
  164.   glFinish ();
  165.   GLwDrawingAreaSwapBuffers (w);
  166.   /* XMesaSwapBuffers (MesaContext (w)); */
  167.       
  168.   xrot += 1.0;
  169.   yrot += 0.7;
  170.   zrot -= 0.3;
  171. }
  172.  
  173.  
  174. static GLint
  175. alloc_color (Widget w, Colormap cmap, int red, int green, int blue)
  176.   XColor xcolor;
  177.   xcolor.red = red;
  178.   xcolor.green = green;
  179.   xcolor.blue = blue;
  180.   xcolor.flags = DoRed | DoGreen | DoBlue;
  181.   if (!XAllocColor (XtDisplay (w), cmap, &xcolor))
  182.     {
  183.       printf ("Couldn't allocate black!\n");
  184.       exit (1);
  185.     }
  186.   return xcolor.pixel;
  187. }
  188.  
  189. /* This is rather inefficient, but we don't mind for the moment,
  190.    because it works.  */
  191.  
  192. static void
  193. translate_pixels (Widget to, Widget from, ...)
  194. {
  195.   va_list ap;
  196.   char *name;
  197.   Colormap from_cmap, to_cmap;
  198.   XColor xcolor;
  199.  
  200.   XtVaGetValues (from, XtNcolormap, &from_cmap, NULL);
  201.   XtVaGetValues (to, XtNcolormap, &to_cmap, NULL);
  202.  
  203.   va_start (ap, from);
  204.   for (name = va_arg (ap, char *); name != NULL; name = va_arg (ap, char *))
  205.     {
  206.       XtVaGetValues (from, name, &xcolor.pixel, NULL);
  207.       XQueryColor (XtDisplay (from), from_cmap, &xcolor);
  208.       if (!XAllocColor (XtDisplay (to), to_cmap, &xcolor))
  209.     XtAppWarning (XtWidgetToApplicationContext (to),
  210.               "Couldn't allocate color!\n");
  211.       else
  212.     XtVaSetValues (from, name, xcolor.pixel, NULL);
  213.     }
  214.   va_end (ap);
  215. }
  216.  
  217. /* Just like the movies: do 24 frames per second. */
  218. unsigned long delay = 1000/24;
  219.  
  220. static void first_frame (Widget);
  221. static void next_frame (XtPointer, XtIntervalId *);
  222.  
  223. static void
  224. first_frame (Widget w)
  225. {
  226.   XtAppAddTimeOut (XtWidgetToApplicationContext (w),
  227.            delay, next_frame, (XtPointer) w);
  228. }
  229.  
  230. static void
  231. next_frame (XtPointer client_data, XtIntervalId *id)
  232. {
  233.   Widget w = (Widget) client_data;
  234.   first_frame (w);
  235.   display (w);
  236. }
  237.  
  238. static XrmOptionDescRec options[] =
  239. {
  240.   { "-debug", "*debug", XrmoptionNoArg, "True" },
  241.   { "-rgba", "*rgba", XrmoptionNoArg, "True" },
  242.   { "-doublebuffer", "*doublebuffer", XrmoptionNoArg, "True" },
  243.   { "-ximage", "*ximage", XrmoptionNoArg, "True" },
  244. };
  245.  
  246. static String fallback_resources[] =
  247. {
  248. #ifndef __GLX_MOTIF
  249.   "*MesaDrawingArea.width: 100",
  250.   "*MesaDrawingArea.height: 100",
  251.   "*MesaDrawingArea.debug: false",
  252.   "*MesaDrawingArea.rgba: false",
  253.   "*MesaDrawingArea.installColormap: true",
  254.   "*MesaDrawingArea.doublebuffer: true",
  255.   "*MesaDrawingArea.ximage: false",
  256.   "*mesa.width: 300",
  257.   "*mesa.height: 300",
  258. #else /* __GLX_MOTIF */
  259.   "*MesaMDrawingArea.width: 100",
  260.   "*MesaMDrawingArea.height: 100",
  261.   "*MesaMDrawingArea.debug: false",
  262.   "*MesaMDrawingArea.rgba: false",
  263.   "*MesaMDrawingArea.installColormap: true",
  264.   "*MesaMDrawingArea.doublebuffer: true",
  265.   "*MesaMDrawingArea.ximage: false",
  266.   "*mesa.width: 300",
  267.   "*mesa.height: 300",
  268. #endif /* __GLX_MOTIF */
  269.   NULL
  270. };
  271.  
  272. int
  273. main (int argc, char *argv[])
  274. {
  275.   Widget top, frame, mesa, mesa1, quit;
  276.   XtAppContext app_context;
  277.   Boolean rgba, cmap_installed;
  278.  
  279.   XtSetLanguageProc (NULL, NULL, NULL);
  280.   top = XtVaAppInitialize (&app_context, "Mcube",
  281.                options, XtNumber (options),
  282.                &argc, argv, fallback_resources,
  283.                NULL);
  284.  
  285. #ifndef __GLX_MOTIF
  286.   frame = XtVaCreateManagedWidget ("frame", formWidgetClass,
  287.                    top,
  288.                    NULL);
  289.   mesa = XtVaCreateManagedWidget ("mesa", mesaDrawingAreaWidgetClass,
  290.                   frame,
  291.                   GLwNshareLists, True,
  292.                   NULL);
  293.   mesa1 = XtVaCreateManagedWidget ("mesa1", mesaDrawingAreaWidgetClass,
  294.                    frame,
  295.                    GLwNshareLists, True,
  296.                    XtNwidth, 100, XtNheight, 100,
  297.                    XtNfromHoriz, mesa, XtNhorizDistance, 10,
  298.                    NULL);
  299.   quit = XtVaCreateManagedWidget ("quit", commandWidgetClass,
  300.                   frame,
  301.                                   XtNfromVert, mesa1, XtNvertDistance, 50,
  302.                                   XtNfromHoriz, mesa, XtNhorizDistance, 10,
  303.                   NULL);
  304.   XtAddCallback (quit, XtNcallback, quit_function, NULL);
  305. #else /* __GLX_MOTIF */
  306.   frame = XtVaCreateManagedWidget ("frame", xmFormWidgetClass,
  307.                    top,
  308.                    /* This should not be necessary.  Is it
  309.                       a bug in LessTif or in my understanding
  310.                       of Motif?  */
  311.                    XtNwidth, 430, XtNheight, 320,
  312.                    NULL);
  313.   mesa = XtVaCreateManagedWidget ("mesa", mesaMDrawingAreaWidgetClass,
  314.                   frame,
  315.                   GLwNshareLists, True,
  316.                   XmNtopAttachment, XmATTACH_FORM,
  317.                   XmNtopOffset, 10,
  318.                   XmNleftAttachment, XmATTACH_FORM,
  319.                   XmNleftOffset, 10,
  320.                   XmNbottomAttachment,  XmATTACH_FORM,
  321.                   XmNbottomOffset, 10,
  322.                   NULL);
  323.   mesa1 = XtVaCreateManagedWidget ("mesa1", mesaMDrawingAreaWidgetClass,
  324.                    frame,
  325.                    GLwNshareLists, True,
  326.                    XtNwidth, 100, XtNheight, 100,
  327.                    XmNtopAttachment, XmATTACH_FORM,
  328.                    XmNtopOffset, 10,
  329.                    XmNleftAttachment, XmATTACH_WIDGET,
  330.                    XmNleftOffset, 10,
  331.                    XmNleftWidget, mesa,
  332.                    XmNrightAttachment, XmATTACH_FORM,
  333.                    XmNrightOffset, 10,
  334.                    NULL);
  335.   quit = XtVaCreateManagedWidget ("quit", xmPushButtonWidgetClass,
  336.                   frame,
  337.                   XmNrightAttachment, XmATTACH_FORM,
  338.                   XmNrightOffset, 10,
  339.                   XmNbottomAttachment, XmATTACH_FORM,
  340.                   XmNbottomOffset, 10,
  341.                   NULL);
  342.   XtAddCallback (quit, XmNarmCallback, quit_function, NULL);
  343. #endif /* __GLX_MOTIF */
  344.  
  345.   XtRealizeWidget (top);
  346.  
  347.   XtVaGetValues (mesa,
  348.          GLwNrgba, &rgba,
  349.          GLwNinstallColormap, &cmap_installed,
  350.          NULL);
  351.   if (rgba)
  352.     {
  353.       Black = Red = Green = Blue = 0;
  354.  
  355.       if (cmap_installed)
  356.     {
  357.       /* In RGBA mode, the Mesa widgets will have their own color map.
  358.          Adjust the colors of the other widgets so that--even if the rest
  359.          of the screen has wrong colors--all application widgets have the
  360.          right colors.  */
  361.              
  362.       translate_pixels (mesa, quit,
  363.                 XtNbackground, XtNforeground, XtNborder, NULL);
  364.       translate_pixels (mesa, frame, XtNbackground, XtNborder, NULL);
  365.  
  366.       /* Finally warp the pointer into the mesa widget, to make sure that
  367.          the user sees the right colors at the beginning.  */
  368.  
  369.       XWarpPointer (XtDisplay (mesa), None, XtWindow (mesa), 0, 0, 0, 0, 0, 0);
  370.     }
  371.     }
  372.   else
  373.     {
  374.       /* Allocate a few colors for use in color index mode.  */
  375.  
  376.       Colormap cmap;
  377.       cmap = DefaultColormap (XtDisplay (top), DefaultScreen (XtDisplay (top)));
  378.       Black = alloc_color (top, cmap, 0x0000, 0x0000, 0x0000);
  379.       Red   = alloc_color (top, cmap, 0xffff, 0x0000, 0x0000);
  380.       Green = alloc_color (top, cmap, 0x0000, 0xffff, 0x0000);
  381.       Blue  = alloc_color (top, cmap, 0x0000, 0x0000, 0xffff);
  382.     }
  383.  
  384.   init_display (mesa);
  385.   init_display (mesa1);
  386.  
  387.   first_frame (mesa);
  388.   first_frame (mesa1);
  389.   
  390.   XtAppMainLoop (app_context);
  391.   return (0);
  392. }
  393.